<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/3/4
 * Time: 下午7:27
 */
$arr = [ 4, 3, 5, 2, 6, 7, 1, 8 ];

function printArray( array $arr ) {
	$count = count( $arr );
	for ( $i = 0; $i < $count; $i ++ ) {
		echo $arr[ $i ] . ' ';
	}
	echo PHP_EOL;
}

function bubbleSort( array $arr ): array {
	$count = count( $arr );
	for ( $i = $count - 1; $i > 0; $i -- ) {
		for ( $j = 0; $j < $i; $j ++ ) {
			if ( $arr[ $j ] > $arr[ $j + 1 ] ) {
				$temp          = $arr[ $j + 1 ];
				$arr[ $j + 1 ] = $arr[ $j ];
				$arr[ $j ]     = $temp;
			}
		}
	}
	
	return $arr;
}

printArray( bubbleSort( $arr ) );